package dao

import (
	"time"
)

// 这是 JwtBlackList 的全局公开dao对象
var JwtBlackListDao = jwtBlackListDao{}

// 这是 jwt_black_list表 的实体类
type JwtBlackList struct {
	Id        int       `json:"id" form:"id" binding:"id"`
	CreatedAt time.Time `json:"created_at" form:"created_at" binding:"created_at"`
	UpdatedAt time.Time `json:"updated_at" form:"updated_at" binding:"updated_at"`
	DeletedAt time.Time `json:"deleted_at" form:"deleted_at" binding:"deleted_at"`
	Token     string    `json:"token" form:"token" binding:"token"`
}

// 注意:这是 JwtBlackList 的私有dao对象
type jwtBlackListDao struct {
	Obj    JwtBlackList
	Insert func(arg JwtBlackList) int          //插入一条记录
	Update func(arg JwtBlackList) int          //更新一条记录
	Delete func(arg JwtBlackList) int          //删除一条记录
	Select func(arg JwtBlackList) JwtBlackList //获取一条记录
	Exist  func(token string) JwtBlackList     `arg:"token"`
}

func (it *jwtBlackListDao) GetRedisJwt(username string) (redisJwt string, err error) {
	redisJwt, err = RedisClient.Get(username).Result()
	return
}

func (it *jwtBlackListDao) SetRedisJwt(jwt, username string) (err error) {
	// 此处过期时间等于jwt过期时间
	t := time.Duration(Cfg.JWT.ExpiresTime) * time.Second
	err = RedisClient.Set(username, jwt, t).Err()
	return err
}
